package com.sztz.oss.aliyun;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.sztz.oss.OssConfig;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * AliYun OSS配置类，用来生成Oss客户端
 * @author yzy
 * @date 2022/10/29 15:36
 */
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties(OssConfig.class)
@ConditionalOnProperty(value = "oss.type", havingValue = "ALIYUN")
public class AliYunOssConfiguration {
    /**
     * 生成AliYun OSS客户端
     * @author yzy
     * @date 2022/10/29 15:36
     */
    @Bean
    public OSS oss(OssConfig ossConfig){
        return new OSSClientBuilder().build(ossConfig.getEndpoint(),ossConfig.getAccessKey(),ossConfig.getSecretKey());
    }

    /**
     * 生成AliYun OSS Api模板
     * @author yzy
     * @date 2022/10/29 15:40
     */
    @Bean
    @ConditionalOnBean(OSS.class)
    @ConditionalOnMissingBean(AliYunOssTemplate.class)
    public AliYunOssTemplate aliYunOssTemplate(OSS oss, OssConfig ossConfig){
        return new AliYunOssTemplate(oss,ossConfig);
    }

}
